package com.minimoment.model.service.impl;

import com.minimoment.base.BaseSupportImpl;
import com.minimoment.model.domain.Movie;
import com.minimoment.model.service.MovieService;
import org.hibernate.Query;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by etund on 2014/11/1.
 */
@Service
@Transactional
@SuppressWarnings("all")
public class MovieServiceImpl extends BaseSupportImpl<Movie> implements MovieService {
    @Override
    public List<Movie> getByFlag(String sign) {
        List<Movie> movies = new ArrayList<Movie>();
//        上头条
        if("head".equals(sign)){
            movies = getSession().createQuery("from Movie m where m.flag=?").setParameter(0,2L).list();
            System.out.println(movies+"===========head    Movies============");
        }
//        正在热播
        if("now".equals(sign)){
            movies = getSession().createQuery("from Movie m where m.flag=?").setParameter(0, 1L).list();
        }
//        即将上映
        if("preview".equals(sign)){
            movies = getSession().createQuery("from Movie m where m.flag=?").setParameter(0,3L).list();
        }
        return movies;
    }

    @Override
    public List<Movie> getByFlag(Long l1,Long l2, Integer i) {
        List<Movie> movies = new ArrayList<Movie>();
        if(i!=null) {
            movies = getSession().createQuery("from Movie m where (m.flag<=?) and (m.flag>=?)order by m.umComemts.size desc ").setParameter(0, l1).setParameter(1, l2).setMaxResults(i).list();
        } else {
            movies = getSession().createQuery("from Movie m where (m.flag<=?) and (m.flag>=?)order by m.umComemts.size desc ").setParameter(0, l1).setParameter(1, l2).list();
        }
        return movies;
    }

    @Override
    public List<Movie> getByTheaterId(String theaterId) {
        list = this.getSession().createQuery("from Movie m join  m.theaters as t where t.id=?").setParameter(0,Long.parseLong(theaterId)).list();
        return list;
    }
}
